# Input:    /Data/finalData.RData
# Output:   /Paper/Figures/pctInterruptions_totalUtterances.pdf
#           /Paper/Figures/pctInterruptions_possibleInteractions.pdf
# Author:   JB


rm(list = ls())
require(lme4)
require(lfe)
require(tidyverse)
require(ggridges)


load('./data/finalData.RData')

# data prep
keeps <- utterance_level %>%
  filter(grepl('FED',respondingTo)) %>%
  filter(!grepl('FED',opensecretsID)) %>%
  group_by(opensecretsID,respondingTo) %>%
  summarise(denom = n(),
            interrupted = sum(interrupted,na.rm=T)/all,
            interruptor = sum(interruptor,na.rm=T)/all) %>%
  ungroup() %>%
  distinct() %>%
  group_by(opensecretsID) %>%
  mutate(n=n()) %>%
  filter(n > 1) %>%
  filter(denom >= 20,
         opensecretsID != respondingTo)

toplot <- utterance_level %>%
  filter(opensecretsID %in% (keeps %>% filter(grepl('YELLEN',respondingTo)) %>% .$opensecretsID),
         ind > mind) %>%
  group_by(opensecretsID,party,gender,name,respondingTo,stab) %>%
  summarise(denom = n(),
            interrupted = sum(interrupted,na.rm=T),
            interruptor = sum(interruptor,na.rm=T),
            interruptedPct = interrupted*100/all,
            interruptorPct = interruptor*100/all) %>%
  ungroup() %>%
  filter(grepl('FED',respondingTo)) %>%
  mutate(id = paste0(str_to_title(name),' [',party,'-',stab,' (',gender,')]')) %>%
  distinct()


pdf('./output/figures/pctInterruptions_totalUtterances.pdf',width = 7,height = 7)
toplot %>%
  mutate(id = factor(id,levels = toplot %>% filter(grepl('YELLEN',respondingTo)) %>%
                       arrange(interruptorPct) %>% .$id),
         Interrupting = ifelse(respondingTo == 'FEDYELLEN','Yellen','Male Fed Chairs')) %>%
  filter(!is.na(id)) %>%
  ggplot(aes(x = interruptorPct,y = id,fill = Interrupting,size = denom,shape = Interrupting)) + 
  geom_point(alpha = .6) + 
  theme_ridges() + 
  scale_size_continuous(name = '# Interactions') + 
  scale_shape_manual(values = c(21,22,24,25)) + 
  xlab('% Interrupting out of Total Utterances') + ylab('') + 
  guides(shape = guide_legend(override.aes = list(size = 4))) + 
  theme(axis.text.y = element_text(size = 8))
dev.off()
